import Message from './message.vue'
import { createVNode, render } from 'vue'

let _timer = null;
let _mountNode = null;
const Msg = function ({ content = '', type = 'info', duration = 1500 }) {
    if (_timer) {
        clearTimeout(_timer);
        Msg.romove();
    }

    const message = createVNode(Message, {
        content: content,
        type: type,
        isShow: true
    });
    _mountNode = document.createElement('div');
    render(message, _mountNode);
    document.body.appendChild(_mountNode);

    // 该去了
    _timer = setTimeout(Msg.romove, duration)
}

Msg.romove = function () {
    document.body.removeChild(_mountNode);
    _timer = null;
}

Msg.success = function (content, duration) {
    Msg({
        content,
        duration,
        type: 'success'
    })
}
Msg.error = function (content, duration) {
    Msg({
        content,
        duration,
        type: 'error'
    })
}

export default Msg